function makeUser() { return { name: "John", ref() { return this; } }; } let user = makeUser(); alert(user.ref().name);Por lo que aprendí de este tutorial , se definen como el objeto antes del punto y dado que este es el valor de retorno de la función, pensé que sería el usuario.
Y este tutorial lo define como el objeto que ejecuta la función actual.
Creo que el objeto que ejecuta la función actual es el usuario. Cuando reemplaza esto por su valor, se convierte en alert(user.user.name); .
El valor de "esto" no se refiere al nombre de un objeto, se refiere al objeto mismo . Entonces no hay "duplicación" aquí, solo está devolviendo un objeto, que resulta ser el mismo objeto con el que comenzó.
Esto es quizás más claro si separas las cosas:
function makeUser() { // Create an object let myObject = { name: "John" }; // Create a function which returns that object let myFunction = function(){ return myObject; }; // Store the function on the object myObject.ref = myFunction; // Return the new object return myObject; } // Create an object let user = makeUser(); // Get the name from the object alert(user.name); // Call the ref() function, which happens to return the same object let otherUser = user.ref(); // Get the name again alert(otherUser.name); // No matter how many times you call .ref() it will return the same object alert(user.ref().ref().ref().ref().name);esto se refiere a un objeto al que pertenece la función función o al objeto ventana si no pertenece a ningún objeto. en este caso, la función ref () devuelve el objeto propietario, por ejemplo,
cuando llames a ref() volverá así
{ name: 'John', ref: [Function: ref] }